package com.leji.song.mainPage;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.dgl.sdk.view.LoadImage;
import com.leji.song.R;
import com.leji.song.mainPage.model.DS_GoodsType;

import java.util.List;

/**
 * Created by mac on 2014/6/14.
 */
public class GoodsTypeAdapter extends BaseAdapter {
    protected ListItemView listItemView;
    protected LayoutInflater listContainer;
    protected Context context;
    protected List<DS_GoodsType> goodsTypeList;

    private int choosedPos = 0;
    private TextView choosedView;

    public interface GoodsTypeClickCallback{
         void onClick(String typeID);
    }

    private GoodsTypeClickCallback callback;
    public GoodsTypeAdapter(){};

    public GoodsTypeAdapter(Context context, List<DS_GoodsType> goodsTypeList, GoodsTypeClickCallback callback){
        this.context = context;
        this.goodsTypeList = goodsTypeList;
        this.callback = callback;

        listContainer = LayoutInflater.from(context);

    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return goodsTypeList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    protected  class ListItemView{
        public TextView name;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 自定义视图
        listItemView = new ListItemView();
        // 获取list_item布局文件的视图
        if (convertView == null) {
            convertView = listContainer.inflate(R.layout.goods_type_adapter, null);
            // 获取控件对象
            listItemView.name = (TextView) convertView.findViewById(R.id.name);

            if(choosedPos == position){
//                listItemView.name.setBackgroundColor(context.getResources().getColor(R.color.white));
                listItemView.name.setBackgroundResource(R.color.white);
                choosedView = listItemView.name;
            }else{
                listItemView.name.setBackgroundResource(R.color.background);
            }
            // 设置控件集到convertView
            convertView.setTag(listItemView);
        } else {
            listItemView = (ListItemView) convertView.getTag();
        }


        listItemView.name.setText(goodsTypeList.get(position).getName());

        listItemView.name.setOnClickListener(new MyClickListener(position));

        return convertView;
    }

    class MyClickListener implements View.OnClickListener {
        private int position;

        public MyClickListener(int position){
            this.position = position;
        }
        @Override
        public void onClick(View v) {

            choosedPos = position;

            choosedView.setBackgroundResource(R.color.background);
            v.setBackgroundResource(R.color.white);

            choosedView = (TextView)v;

            callback.onClick(goodsTypeList.get(position).getId());

        }

    }

}
